﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using CityDashboard.Common;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Item Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234232

namespace CityDashboard
{
    /// <summary>
    /// A page that displays details for a single item within a group while allowing gestures to
    /// flip through other items belonging to the same group.
    /// </summary>
    public sealed partial class ItemTrackingDetailPage1 : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        MainViewModel mainModel;

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public ItemTrackingDetailPage1()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            ProggressBarVisible(true);
            try
            {
                mainModel = new MainViewModel();
                this.DataContext = mainModel;

            }
            finally
            {
                ProggressBarVisible(false);
            }
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            object navigationParameter;
            if (e.PageState != null && e.PageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = e.PageState["SelectedItem"];
            }

            // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
            // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]
            // TODO: Assign the selected item to this.flipView.SelectedItem
        }

        private void ProggressBarVisible(bool visible)
        {
            ProgressBarLoad.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
            ProgressBarLoad.IsIndeterminate = visible;
        }

        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
            pageTitle.Text = e.Parameter as string;
            if (pageTitle.Text.Length == 3)
            {
               
                int p1 = Convert.ToInt32(pageTitle.Text.Substring(0,1));
                int p2 = Convert.ToInt32(pageTitle.Text.Substring(1,1));
                int p3 = Convert.ToInt32(pageTitle.Text.Substring(2,1));
                mainModel.add_View_HSDetailFilter_HPT(p1, p2, p3);
            }
            else
            {
                mainModel.add_View_HSTrackingDetailHPT(e.Parameter as string);
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        private void SelectItemGridChanged(object sender, DevExpress.UI.Xaml.Grid.SelectedItemChangedEventArgs e)
        {
           // string str = RadDataGrid2.GetSelectedItemCellValue("Trangthai_hs").ToString() + RadDataGrid2.GetSelectedItemCellValue("Xuly").ToString();
            mainModel.add_View_HSTrackingDetailOneHSHPT(RadDataGrid2.GetSelectedItemCellValue("Sobnhan").ToString());
            if (!logincontrol1.IsOpen)
            {
               // RootPopupBorder.Width = 646;
                logincontrol1.HorizontalOffset = Window.Current.Bounds.Width - 710;
               // logincontrol1.VerticalOffset = Window.Current.Bounds.Height - 1000;
                logincontrol1.IsOpen = true;
            }
        }

        private void TrackingPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (pageTitle.Text.Length == 3)
            {
                RadDataGrid2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                HsFilterbysobnhan.Visibility = Windows.UI.Xaml.Visibility.Visible;
                DuandautuByNhadautu.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                DuandautuByQuocgia.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                DuandautuByQuocgiaUSD.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                GridHSDetail.Visibility = Windows.UI.Xaml.Visibility.Visible;
                pageTitle.Text = "Thông tin chi tiết hồ sơ";
                Scrollviewer2.Visibility = Windows.UI.Xaml.Visibility.Visible;
                Scrollviewer1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else
            {
                if (pageTitle.Text == "2")
                {
                    pageTitle.Text = "Thông tin chi tiết các quốc gia";
                    RadDataGrid2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    HsFilterbysobnhan.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByNhadautu.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgia.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    GridHSDetail.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgiaUSD.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                }
                else if (pageTitle.Text == "4")
                {
                    pageTitle.Text = "Thông tin chi tiết các quốc gia";
                    RadDataGrid2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    HsFilterbysobnhan.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByNhadautu.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgia.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    GridHSDetail.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgiaUSD.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    Scrollviewer1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

                }
                else if (pageTitle.Text == "3")
                {
                    pageTitle.Text = "Thông tin chi tiết các nhà đầu tư";
                    RadDataGrid2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    HsFilterbysobnhan.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByNhadautu.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    DuandautuByQuocgia.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    GridHSDetail.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgiaUSD.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
                else
                {
                    RadDataGrid2.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    HsFilterbysobnhan.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    DuandautuByNhadautu.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgia.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    GridHSDetail.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    DuandautuByQuocgiaUSD.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    Scrollviewer1.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    Scrollviewer2.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    pageTitle.Text = "Thông tin chi tiết hồ sơ";
                }
            }
        }

        private void SelectItemGridDetailChanged(object sender, DevExpress.UI.Xaml.Grid.SelectedItemChangedEventArgs e)
        {
            // string str = RadDataGrid2.GetSelectedItemCellValue("Trangthai_hs").ToString() + RadDataGrid2.GetSelectedItemCellValue("Xuly").ToString();
            mainModel.add_View_HSTrackingDetailOneHSHPT(GridHSDetail.GetSelectedItemCellValue("Sobnhan").ToString());
            if (!logincontrol1.IsOpen)
            {
                // RootPopupBorder.Width = 646;
                logincontrol1.HorizontalOffset = Window.Current.Bounds.Width - 710;
                // logincontrol1.VerticalOffset = Window.Current.Bounds.Height - 1000;
                logincontrol1.IsOpen = true;
            }
        }

        private void dataGrid1_AutoGeneratingColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Id")
            {
                e.Column.Header = "STT";
                e.Column.Width = 100;
            }

            if (e.Column.FieldName == "Sobnhan")
            {
                e.Column.Header = "Số biên nhận";
                e.Column.Width = 200;
            }
            if (e.Column.FieldName == "Tendn")
            {
                e.Column.Header = "Tên doanh nghiệp";
                 e.Column.Width = 400;
            }

            if (e.Column.FieldName == "Tenda")
            {
                e.Column.Header = "Tên dự án";
                 e.Column.Width = 400;
            }

            if (e.Column.FieldName == "Ngaynhan")
            {
                e.Column.Header = "Ngày nhận";
                 e.Column.Width = 200;
            }

            if (e.Column.FieldName == "Ngaytra")
            {
                e.Column.Header = "Ngày hẹn trả";
                 e.Column.Width = 200;
            }
            if (e.Column.FieldName == "Trangthai_hs")
            {
                e.Column.Header = "Trạng Thái";
                e.Column.Width = 180;
            }
            if (e.Column.FieldName == "Xuly")
            {
                e.Column.Header = "Tình Trạng";
                e.Column.Width = 150;
            }

        }

        private void GridHSDetail_ReCreateColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Id")
            {
                e.Column.Header = "STT";
                e.Column.Width = 100;
            }

            if (e.Column.FieldName == "Sobnhan")
            {
                e.Column.Header = "Số biên nhận";
                e.Column.Width = 200;
            }
            if (e.Column.FieldName == "Tendn")
            {
                e.Column.Header = "Tên doanh nghiệp";
                e.Column.Width = 400;
            }

            if (e.Column.FieldName == "Tenda")
            {
                e.Column.Header = "Tên dự án";
                e.Column.Width = 400;
            }

            if (e.Column.FieldName == "Ngaynhan")
            {
                e.Column.Header = "Ngày nhận";
                e.Column.Width = 200;
            }

            if (e.Column.FieldName == "Ngaytra")
            {
                e.Column.Header = "Ngày hẹn trả";
                e.Column.Width = 200;
            }
            if (e.Column.FieldName == "Trangthai_hs")
            {
                e.Column.Header = "Trạng Thái";
                e.Column.Width = 180;
            }
            if (e.Column.FieldName == "Xuly")
            {
                e.Column.Header = "Tình Trạng";
                e.Column.Width = 150;
            }
        }

        private void DuandautuByQuocgia_GenerateColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Id")
            {
                e.Column.Header = "STT";
                e.Column.Width = 50;
            }

            if (e.Column.FieldName == "Tenquoctich_BC")
            {
                e.Column.Header = "Tên quốc gia";
                //e.Column.Width = 50;
            }
            if (e.Column.FieldName == "SL")
            {
                e.Column.Header = "Tổng dự án";
               // e.Column.Width = 500;
            }
        }

        private void DuandautuByNhadautu_GenerateColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Sobnhan")
            {
                e.Column.Header = "STT";
                e.Column.Width = 50;
            }
            if (e.Column.FieldName == "Tenduandautu")
            {
                e.Column.Header = "Nhà đầu tư";
                e.Column.Width = 500;
            }
            if (e.Column.FieldName == "TongvonUSD")
            {
                e.Column.Header = "Tổng (Triệu USD)";
               // e.Column.Width = 100;
            }
        }

        private void HsFilterbysobnhan_CreateColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Trangthai_hs")
            {
                e.Column.Header = "Trạng Thái";
                //e.Column.Width = 180;
            }

            if (e.Column.FieldName == "NgayXuly")
            {
                e.Column.Header = "Ngày xử lý";
                //e.Column.Width = 180;
            }

            if (e.Column.FieldName == "TenNhanVien")
            {
                e.Column.Header = "Tên nhân viên";
                //e.Column.Width = 180;
            }
        }

        private void QuocTichDetail_BC_USD_recreatecolumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Name")
            {
                e.Column.Header = "Tên quốc gia";
                //e.Column.Width = 50;
            }
            if (e.Column.FieldName == "Sl")
            {
                e.Column.Header = "Tổng vốn(Triệu USD)";
                // e.Column.Width = 500;
            }
        }
    }
}
